package main

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

var db *gorm.DB
var cnt int

func main() {
	r := gin.Default()
	rv := r.Group("api")
	rv.GET("/count", func(c *gin.Context) {
		//if count,err:=getCount();err!=nil{
		//	c.JSON(200,gin.H{
		//		"status":501,
		//		"msg":err.Error(),
		//	})
		//}else {
		//	c.JSON(200, gin.H{
		//		"status":0,
		//		"data": gin.H{
		//			"count": count,
		//		},
		//	})
		//}
		cnt++
		c.JSON(200, gin.H{
			"status": 0,
			"data": gin.H{
				"count": cnt,
			},
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

//func init() {
//	var err error
//	db,err=gorm.Open(postgres.Open("user=postgres password=Qwer4321! host=db port=5432 dbname=ynfclsp sslmode=disable TimeZone=Asia/Shanghai"))
//	if err!=nil{
//		log.Panic(err)
//	}
//}

func getCount() (int, error) {
	var count int
	db.Debug().Exec("update temp_count set count=count+1")
	err := db.Debug().Raw("select count from temp_count").Scan(&count).Error
	return count, err
}
